package max.ubitest.pipe
{
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	
	import max.ubitest.pipe.commands.ClearCellsAroundCommand;
	import max.ubitest.pipe.commands.ClearCommand;
	import max.ubitest.pipe.commands.ClickCellCommand;
	import max.ubitest.pipe.commands.MappedComponentSelectedCommand;
	import max.ubitest.pipe.commands.SelectComponentCommand;
	import max.ubitest.pipe.commands.UnselectComponentCommand;
	import max.ubitest.pipe.commands.ValidationCommand;
	import max.ubitest.pipe.events.PipeEvent;
	import max.ubitest.pipe.locators.CellLocator;
	import max.ubitest.pipe.locators.ComponentsLocator;
	import max.ubitest.pipe.view.CellContainer;
	import max.ubitest.pipe.view.CellContainerMediator;
	import max.ubitest.pipe.view.CellRenderer;
	import max.ubitest.pipe.view.CellRendererMediator;
	import max.ubitest.pipe.view.CompContainer;
	import max.ubitest.pipe.view.CompContainerMediator;
	import max.ubitest.pipe.view.CompSelector;
	import max.ubitest.pipe.view.CompSelectorMediator;
	import max.ubitest.pipe.view.ComponentRenderer;
	import max.ubitest.pipe.view.ComponentRendererMediator;
	import max.ubitest.pipe.view.Map;
	import max.ubitest.pipe.view.SelectorRenderer;
	import max.ubitest.pipe.view.SelectorRendererMediator;
	import max.ubitest.pipe.view.ToolBar;
	import max.ubitest.pipe.view.ToolBarMediator;
	
	import mx.controls.Button;
	import mx.core.IVisualElementContainer;
	
	import org.robotlegs.mvcs.Context;
	
	public class PipeContext extends Context
	{
		private var map:Map;
		private var selector:CompSelector;
		private var cancelButton:Button;
		
		public function PipeContext(contextView:DisplayObjectContainer=null, autoStartup:Boolean=true)
		{
			super(contextView, autoStartup);
		}
		
		override public function startup():void{
			injector.mapSingleton(ComponentsLocator);
			injector.mapSingleton(CellLocator);
			
			mediatorMap.mapView(CellContainer, CellContainerMediator);
			mediatorMap.mapView(CellRenderer, CellRendererMediator);
			mediatorMap.mapView(CompContainer, CompContainerMediator);
			mediatorMap.mapView(CompSelector, CompSelectorMediator);
			mediatorMap.mapView(ComponentRenderer, ComponentRendererMediator);
			mediatorMap.mapView(SelectorRenderer, SelectorRendererMediator);
			mediatorMap.mapView(ToolBar, ToolBarMediator);
			
			commandMap.mapEvent(PipeEvent.CELL_SELECTED, ClickCellCommand);
			commandMap.mapEvent(PipeEvent.COMPONENT_SELECTED, SelectComponentCommand);
			commandMap.mapEvent(PipeEvent.MAPPED_COMPONENT_SELECTED, MappedComponentSelectedCommand);
			commandMap.mapEvent(PipeEvent.DO_VALIDATION, ValidationCommand);
			commandMap.mapEvent(PipeEvent.CLEAR_CELLS_AROUND, ClearCellsAroundCommand);
			commandMap.mapEvent(PipeEvent.COMPONENT_UNSELECTED, UnselectComponentCommand);
			commandMap.mapEvent(PipeEvent.CLEAR, ClearCommand);
			
			map = new Map();
			selector = new CompSelector();
			selector.y = 550;
			cancelButton = new Button();
			cancelButton.label = "Clear";
			cancelButton.x = 550;
			cancelButton.y = 520;
			cancelButton.addEventListener(MouseEvent.CLICK, onClear);
			(contextView as IVisualElementContainer).addElement(cancelButton);
			(contextView as IVisualElementContainer).addElement(map);
			(contextView as IVisualElementContainer).addElement(selector);
		}
		
		private function onClear(e:MouseEvent):void{
			eventDispatcher.dispatchEvent(new PipeEvent(PipeEvent.CLEAR, null));
		}
	}
}